package com.cy.container;

import com.cy.loader.MyClassLoader;
import com.cy.server.Request;
import com.cy.server.Response;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author ChenYan
 * @Date 2021/8/13 15:26
 * @Description
 */
public class Wrapper {

    /**
     * servlet全路径
     */
    private String servletClass;

    /**
     * class所在的路径
     */
    private String path;

    private Object obj;

    private Method method;

    /**
     * Gets the value of servletClass. *
     *
     * @return the value of servletClass
     */
    public String getServletClass() {
        return servletClass;
    }

    /**
     * Sets the servletClass. *
     * <p>You can use getServletClass() to get the value of servletClass</p>
     * * @param servletClass servletClass
     */
    public void setServletClass(String servletClass) {
        this.servletClass = servletClass;
    }

    /**
     * Gets the value of path. *
     *
     * @return the value of path
     */
    public String getPath() {
        return path;
    }

    /**
     * Sets the path. *
     * <p>You can use getPath() to get the value of path</p>
     * * @param path path
     */
    public void setPath(String path) {
        this.path = path;
    }

    /**
     * Gets the value of obj. *
     *
     * @return the value of obj
     */
    public Object getObj() {
        return obj;
    }

    /**
     * Sets the obj. *
     * <p>You can use getObj() to get the value of obj</p>
     * * @param obj obj
     */
    public void setObj(Object obj) {
        this.obj = obj;
    }

    /**
     * Gets the value of method. *
     *
     * @return the value of method
     */
    public Method getMethod() {
        return method;
    }

    /**
     * Sets the method. *
     * <p>You can use getMethod() to get the value of method</p>
     * * @param method method
     */
    public void setMethod(Method method) {
        this.method = method;
    }

    public void invokeService(Request request, Response response) throws InvocationTargetException, IllegalAccessException {
        if (this.method == null){
            MyClassLoader.getInstance().loadClassAndMethod(this);
        }
        this.getMethod().invoke(this.obj, request, response);
    }
}
